#pragma once

#include "gtl/directui/window.h"

template<class Interface>
class CMessageHandler
	: public gtl::dui::window
	, public Interface
{
public:
	CMessageHandler()
	{
		create(NULL, _T("MessageHandler"), _T("MessageHandler"), WS_POPUP, NULL, 0, 0, 0, 0);
	}

	virtual ~CMessageHandler()
	{
	}

public:
	virtual BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lResult, DWORD dwMsgMapID = 0)
	{
		switch(uMsg)
		{
		case WM_CLOSE:
			Release();
			return TRUE;

		default:
			if(OnMessage(uMsg, wParam, lParam))
				return TRUE;
			break;
		}

		return gtl::dui::window::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult, dwMsgMapID);
	}

	//virtual BOOL PreTranslateMessage(MSG* pMsg)
	//{
	//	switch(pMsg->message)
	//	{
	//	case WM_CLOSE:
	//		Release();
	//		return TRUE;

	//	default:
	//		if(OnMessage(pMsg->message, pMsg->wParam, pMsg->lParam))
	//			return TRUE;
	//		break;
	//	}

	//	return CWnd::PreTranslateMessage(pMsg);
	//}

protected:
	void Release()
	{
		destroy();
		delete this;
	}

	virtual BOOL OnMessage(UINT message, WPARAM wParam, LPARAM lParam)
	{
		return FALSE;
	}
};